//
//  DouyaIndicator.m
//  指示器
//
//  Created by douya on 15/1/14.
//  Copyright (c) 2015年 douya. All rights reserved.
//

#import "DouyaIndicator.h"

@implementation DouyaIndicator
-(id)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame])
    {
        self.backgroundColor=[UIColor colorWithWhite:0.5 alpha:0.5];
//        _isAnimating=NO;
        
        _imageView=[[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/2-frame.size.width/8, frame.size.height/2-frame.size.height/8, frame.size.width/4, frame.size.height/4-20)];
        [self addSubview:_imageView];
        
        //设置动画帧
        _imageView.animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"01"],[UIImage imageNamed:@"2"],[UIImage imageNamed:@"3"],[UIImage imageNamed:@"4"],[UIImage imageNamed:@"5"],[UIImage imageNamed:@"6"],nil];
        
        _infoLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_imageView.frame)-20, frame.size.width, 20)];
        _infoLabel.backgroundColor=[UIColor clearColor];
        _infoLabel.textAlignment=NSTextAlignmentCenter;
        _infoLabel.textColor=[UIColor colorWithRed:84.0/255 green:86.0/255 blue:212.0/255 alpha:1];
        _infoLabel.font=[UIFont fontWithName:@"ChalkboardSE-Bold" size:17.0f];
        [self addSubview:_infoLabel];
        self.layer.hidden=YES;//先将图片隐藏
        
    }
    return self;
}


#pragma mark- 开始动画
-(void)beginAnimation
{
//    _isAnimating=YES;
    self.layer.hidden=NO;//开始动画时就不隐藏
    [self doAnimation];
}

-(void)doAnimation
{
    _infoLabel.text=_loadText;
    //设置动画时间
    _imageView.animationDuration=1.0;
    //设置重复次数，0表示永远重复
    _imageView.animationRepeatCount=0;
    //开始动画
    [_imageView startAnimating];
}
#pragma mark- 加载成功或者失败

-(void)stopAnimationWithLoadText:(NSString *)text withType:(BOOL)type
{
//    _isAnimating=NO;
    _infoLabel.text=text;
    if (type)//如果加载成功
    {
        [UIView animateWithDuration:0.3f animations:^{
            self.alpha=0;//动画效果是逐渐变淡
        }completion:^(BOOL finished){
            [_imageView stopAnimating];//完全变淡以后 就停止动画
            self.layer.hidden=YES;//然后将其隐藏
            self.alpha=1;//将其view的alpha设回1
        }];
    }else//如果加载失败
    {
        [_imageView stopAnimating];//动画停止
        [_imageView setImage:[UIImage imageNamed:@"3"]];//画面停留在3.png上
    }
}

-(void)setLoadText:(NSString *)text//它不是设置有设置标题的属性吗？为什么不直接操作属性而要再创建一个方法呢？  答：这是需要的  因为需要判断输入的标题是否有内容
{
    if (text)//如果text不为空
    {
        _loadText=text;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
